/*
 * Copyright 2007-2008 Andrew O'Malley
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package ws.quokka.plugin.help;

import ws.quokka.core.util.URLs;
import ws.quokka.core.util.xml.Document;
import ws.quokka.core.util.xml.Element;

import java.net.URL;

import java.util.Iterator;
import java.util.List;


/**
 *
 */
public class XmlCatalogHelpGenerator extends AbstractHelpGenerator {
    //~ Methods --------------------------------------------------------------------------------------------------------

    protected URL getMetadataURL() {
        return URLs.toURL(artifact.getLocalCopy(), plugin.getCatalogPath(artifact.getId()));
    }

    protected void addContent(Document out, Element bodyEl) {
        Element catlogEl = metaDocument.getRoot();
        List entries = catlogEl.getChildren("public");

        if (entries.size() > 0) {
            bodyEl.addChild("h2").addText("Catalogue Public Entries");

            Element tableEl = bodyEl.addChild("table");
            Element trEl = tableEl.addChild("tr");
            trEl.addChild("th").addText("Public Id");
            trEl.addChild("th").addText("URI");

            for (Iterator i = entries.iterator(); i.hasNext();) {
                Element publicEl = (Element)i.next();
                trEl = tableEl.addChild("tr");
                trEl.addChild("td").addText(publicEl.getAttribute("publicId"));
                trEl.addChild("td").addText(publicEl.getAttribute("uri"));
            }
        }

        entries = catlogEl.getChildren("uri");

        if (entries.size() > 0) {
            bodyEl.addChild("h2").addText("Catalogue URI Entries");

            Element tableEl = bodyEl.addChild("table");
            Element trEl = tableEl.addChild("tr");
            trEl.addChild("th").addText("Name");
            trEl.addChild("th").addText("URI");

            for (Iterator i = entries.iterator(); i.hasNext();) {
                Element publicEl = (Element)i.next();
                trEl = tableEl.addChild("tr");
                trEl.addChild("td").addText(publicEl.getAttribute("name"));
                trEl.addChild("td").addText(publicEl.getAttribute("uri"));
            }
        }
    }
}
